package com.blrs.service.maintype.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.MainType;
import com.blrs.mapper.MainTypeMapper;
import com.blrs.service.maintype.IMainTypeService;
import com.blrs.system.service.ISysDeptService;
import com.blrs.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 主分类Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-09-04
 */
@Service
public class MainTypeServiceImpl implements IMainTypeService
{
    @Autowired
    private MainTypeMapper mainTypeMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询主分类
     * 
     * @param id 主分类主键
     * @return 主分类
     */
    @Override
    public MainType selectMainTypeById(String id)
    {
        return mainTypeMapper.selectMainTypeById(id);
    }

    /**
     * 查询主分类列表
     * 
     * @param mainType 主分类
     * @return 主分类
     */
    @Override
    public List<MainType> selectMainTypeList(MainType mainType)
    {

        return mainTypeMapper.selectMainTypeList(mainType);
    }

    @Override
    public List<MainType> selectMainTypeListOther(MainType mainType) {
        return  mainTypeMapper.selectMainTypeListOther(mainType);
    }

    @Override
    public List<Map> getTree(MainType mainType) {
        if(!"admin".equals(SecurityUtils.getUsername())){
            mainType.setBelongTo(SecurityUtils.getOrgId());
        }
        List<MainType> mainTypes=mainTypeMapper.selectMainTypeList(mainType);
        List<MainType> fList=mainTypes.stream().filter(obj->obj.getParentId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<MainType> zList=mainTypes.stream().filter(obj->!obj.getParentId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<Map> list=new ArrayList<>();
        for (MainType info : fList) {
            Map map=new HashMap<>();
            map.put("id",info.getId());
            map.put("idPath",info.getIdPath());
            map.put("parentId",info.getParentId());
            map.put("label",info.getName());
            List<Map> listMap=list(zList,info.getId());
            if(listMap.size()!=0){
                map.put("children",listMap);
            }
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Map> getTreeOther(MainType mainType) {
        if(!"admin".equals(SecurityUtils.getUsername())){
            mainType.setBelongTo(SecurityUtils.getOrgId());
        }
        List<MainType> mainTypes=mainTypeMapper.selectMainTypeListOther(mainType);
        List<MainType> fList=mainTypes.stream().filter(obj->obj.getParentId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<MainType> zList=mainTypes.stream().filter(obj->!obj.getParentId().equals("00000000-0000-0000-0000-000000000000")).collect(Collectors.toList());
        List<Map> list=new ArrayList<>();
        for (MainType info : fList) {
            Map map=new HashMap<>();
            map.put("id",info.getId());
            map.put("idPath",info.getIdPath());
            map.put("parentId",info.getParentId());
            map.put("label",info.getName());
            List<Map> listMap=list(zList,info.getId());
            if(listMap.size()!=0){
                map.put("children",listMap);
            }
            list.add(map);
        }
        return list;
    }

    private List<Map> list(List<MainType> zList, String parentId){
        List<Map> list=new ArrayList<>();
        for (MainType info : zList) {
            if(info.getParentId().equals(parentId)){
                Map map=new HashMap<>();
                map.put("id",info.getId());
                map.put("idPath",info.getIdPath());

                map.put("label",info.getName());
                List<Map> listMap=list(zList,info.getId());
                if(listMap.size()!=0){
                    map.put("children",listMap);
                }
                list.add(map);
            }
        }
        return list;
    }
    /**
     * 新增主分类
     * @param mainType 主分类
     * @return 结果
     */
    @Override
    public int insertMainType(MainType mainType)
    {
        int size=mainTypeMapper.size(mainType);
        if(size!=0){
            throw new ServiceException("分类名称不能重复！");
        }
        mainType.setDeptName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        mainType.setDeptId(String.valueOf(SecurityUtils.getDeptId()));
        mainType.setOpId(SecurityUtils.getUserId());
        mainType.setOpName(SecurityUtils.getUsername());
        mainType.setBelongTo(SecurityUtils.getOrgId());
        return mainTypeMapper.insertMainType(mainType);
    }

    /**
     * 修改主分类
     * 
     * @param mainType 主分类
     * @return 结果
     */
    @Override
    public int updateMainType(MainType mainType)
    {
        return mainTypeMapper.updateMainType(mainType);
    }

    /**
     * 批量删除主分类
     * 
     * @param ids 需要删除的主分类主键
     * @return 结果
     */
    @Override
    public int deleteMainTypeByIds(String[] ids)
    {
        return deleteMainTypeById(ids[0]);
    }

    /**
     * 删除主分类信息
     * 
     * @param id 主分类主键
     * @return 结果
     */
    @Override
    public int deleteMainTypeById(String id)
    {
        MainType type=mainTypeMapper.selectMainTypeById(id);
        String label=sysDictDataService.selectDictLabel("main_type",type.getTypeCode());
        if(!StringUtils.isEmpty(label)) {
            //int count=mainTypeMapper.count(id,label);
            //兼容之前业务没有idPath的
            int counted=mainTypeMapper.countNoPath(id,label);
            if(counted!=0){
                throw new ServiceException("此分类下有业务数据，不能删除！");
            }
        }
        return mainTypeMapper.deleteMainTypeById(id);
    }

    @Override
    public List<MainType> selectLikeId(String id) {
        return mainTypeMapper.selectLikeId(id);
    }


}
